
define(['vue','config','common'],function(Vue,config,utils){
	'use strict';

	var TwoPasswordView=new Vue({
		el:'#app',
		data:{
			password:'',
			repeatpassword:'',
			userPhone:'',
			phoneCode:'',
			veryCode:'',
			secondOfDelay : 60000 ,
			secondLeftOfDelay : 0 
		},
		created:function(){	
			utils.userToken();
			this.typeName = utils.getData('typeName');
			var _this = this;
			utils.ajax(utils.getWebUrl('user/personal'),null,'POST',function(res){
				//console.log(res)
				_this.userPhone = res.data.phone;
			})
		},
		methods:{
			logout:function(event){
				utils.logout();
			},
			settingTowpsw:function(){
				if($.trim(this.password) === ""){
					utils.appAlert('请输入二级密码');
                    return;
				}

				if($.trim(this.repeatpassword) === ""){
					utils.appAlert('请再次确认二级密码');
                    return;
				}

				if(this.password !== this.repeatpassword){
					utils.appAlert('两次密码输入不一致');
                    return;
				}
				
				if(!(/^.{6,20}$/.test(this.password))){
					utils.appAlert('密码只能在6~20位之间');
                    return;
				}

				if($.trim(this.veryCode) === ""){
					utils.appAlert('验证码不能为空');
                    return;
				}

				utils.ajax(utils.getWebUrl('user/setTwoPwd'),{
					password:this.password,
					phoneCode:this.veryCode
				},'POST',function(res){
					
					 utils.appAlert('设置成功',function(){
					 	window.location.href="Settings.html"
					 });

				})
			},
			getVeryCode:function(){

				if($.trim(this.password) === ""){
					utils.appAlert('请输入二级密码');
                    return;
				}

				if($.trim(this.repeatpassword) === ""){
					utils.appAlert('请再次确认二级密码');
                    return;
				}

				if(this.password !== this.repeatpassword){
					utils.appAlert('两次密码输入不一致');
                    return;
				}

				if(!(/^.{6,20}$/.test(this.password))){
					utils.appAlert('密码只能在6~20位之间');
                    return;
				}

				var _this=this;
				var data = {
					phone: _this.userPhone,
					codeType: 5,
					smsType: 1
				};
				utils.initAjax('verify/sendPhoneCode',data,'POST',function(res){
			        utils.appAlert('请查收你的短信',function(){
			        	_this.sendVerifyCodeTimer();
			        });
			        $(".verification").attr('disabled','true');
				})
			},
			/*getvoiceMessage:function(codeType){
				var _this = this;
				var data = {
					phone: _this.userPhone,
					codeType: 5,
					smsType: 2
				};
				utils.initAjax('verify/sendPhoneCode',data,'POST',function(res){
			        utils.appAlert('你正在使用语音获取验证码<br/>请保持手机通畅');
				})
			},*/
			sendVerifyCodeTimer:function(){
				var $time_debounce = $('.verification');
				var _this=this;
				_this.setTimeToDefaut = function(secondsOfInterval) {
                	this.secondLeftOfDelay = secondsOfInterval;
            	};

				var secondsOfInterval = this.secondOfDelay;
				var intervalID = setInterval(function() {
	                secondsOfInterval = secondsOfInterval - 1000;
	                $time_debounce.html(" " + secondsOfInterval / 1000 + " 秒 ")
	                if (secondsOfInterval <= 0) {
	                    clearInterval(intervalID);
	                    $(".verification").removeAttr("disabled");
	                    $time_debounce.html("获取验证码");
	                }
	                _this.setTimeToDefaut(secondsOfInterval);
	            }, 1000);
			}
		}
	})

	return TwoPasswordView
});